package com.medistat.controllers;

import java.util.Calendar;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.medistat.dialog.services.DialogService;
import com.medistat.discretevalue.services.DiscreteValueProvider;
import com.medistat.model.enums.Days;
import com.medistat.model.enums.TypeOfWork;
import com.medistat.services.UserContextProvider;
import com.medistat.util.DateUtils;
import com.medistat.util.DaysUtils;
import com.medistat.util.TypeOfWorkUtils;

public abstract class AbstractController {

	//services
	protected UserContextProvider userContextProvider;
	private DiscreteValueProvider discreteValueProvider;
	private DialogService dialogService;
	
	//state
	private String departmentId;
	
	@Autowired
	public void setUserContextProvider(UserContextProvider userContextProvider) {
		this.userContextProvider = userContextProvider;
	}
	
	@Autowired
	public void setDiscreteValueProvider(DiscreteValueProvider discreteValueProvider) {
		this.discreteValueProvider = discreteValueProvider;
	}
	
	@Autowired
	public void setDialogService(DialogService dialogService) {
		this.dialogService = dialogService;
	}

	
	public DiscreteValueProvider getDiscreteValueProvider() {
		return discreteValueProvider;
	}
	
	public DialogService getDialogService() {
		return dialogService;
	}
	
//	public String checkIfUSerIsLogedIn() {
//		
//		if(userContextProvider.getUserContext()==null || userContextProvider.getUserContext().getCredentials()==null) {
//			FacesContext fc = FacesContext.getCurrentInstance();
//			
//			ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler();
//			nav.performNavigation(UrlConsts.UNAUTHORIZED_PAGE);
//		} 
//		return getPageName();
//	}
	
	private void addMessage(String message, Severity severity) {
		if(message==null) {
			return;
		}
		FacesContext fc = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage();
		facesMessage.setSummary(message);
		facesMessage.setSeverity(severity);
		fc.addMessage(null, facesMessage);
	}
	
	public void login(String username, String password) throws Exception {
//		User user = dialogService.getUser(username, password);
//		Credentials credentials = new Credentials(user.getUsername(), user.getPassword());
//		userContextProvider.getUserContext().setCredentials(credentials);
	}
	
	public void reLogin(String username, String newPassword) throws Exception {
//		if (userContextProvider.getUserContext().isUserLogedIn()) {
//			userContextProvider.getUserContext().setCredentials(null);
//		}
//		login(username, newPassword);
	}
	
	public String convertDate(Calendar date) {
		return DateUtils.convertCalendarToStringDate(date);
	}
	
	public String sortByDate(Calendar date) {
		return DateUtils.convertCalendarToStringDateYearFirst(date);
	}
	
	public String getDayDescription(Days day) {
		return DaysUtils.getDaysDescription(day);
	}
	
	public String getTypeOfWorkDescription(TypeOfWork typeOfWork) {
		return TypeOfWorkUtils.getTypeOfWorKDescription(typeOfWork);
	}
	
	public void addInfo(String message) {
		addMessage(message, FacesMessage.SEVERITY_INFO);
	}
	
	public void addError(String message) {
		addMessage(message, FacesMessage.SEVERITY_ERROR);
	}
	
	public void addWarning(String message) {
		addMessage(message, FacesMessage.SEVERITY_WARN);
	}
	
	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}
	
	public abstract String getPageName();
}
